<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
if($_RQ['op']=='main'){
	$return=array();
	$return['sale_live']=$this->plugin_setting('sale_live');
	$id=intval($_RQ['id']);
	$live_id=intval($_RQ['live_id']);
	if($live_id){
		$this->session('live_id',$live_id);
	}
	if($_SESSION['wxapp_id']){
		$live_id=pdo_getcolumn('core_account_wxapp_live',array('wxapp_id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']),'id');
		$this->session('live_id',$live_id);
	}
	$live_id=$this->session('live_id');
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$roomsnum=pdo_count('core_account_wxapp_live_rooms',array('mid'=>$_SESSION['uid'],'live_id'=>$live_id,'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime));
	if(intval($return['sale_live']['openrooms_num']) && $roomsnum>=intval($return['sale_live']['openrooms_num'])){
		exi('您今天已经创建'.$roomsnum.'间了，不能再创建了，明天再来吧！','error');
	}
	$return['member']=model('core/member')->detail($_SESSION['uid'],array('sex','credit1','credit2','mobile','avatar','nickname'));
	if($id){
		$item=pdo_get('core_account_wxapp_live_rooms',array('id'=>$id));
		$item['start_time'] =$item['start_time']?date('Y-m-d',$item['start_time']):'16:00';
		$item['end_time'] =$item['end_time']?date('Y-m-d',$item['end_time']):'18:00';
		$item['time']=$item['start_time'].'~'.$item['end_time'];
	}else{
		$item=array('live_id'=>$live_id);
		$item['start_time'] =$item['start_time']?:'16:00';
		$item['end_time'] =$item['end_time']?:'18:00';
		$item['time']=$item['start_time'].'~'.$item['end_time'];
		$item['anchor_name']=$return['member']['nickname'];
		$item['sex']=$return['member']['sex'];
	}
	$return['item']=$item;
	$return['types']=array('0'=>'手机直播','1'=>'推流');
	exi($return);
}
if($_RQ['op']=='post'){
	$sale_live=$this->plugin_setting('sale_live');
		$post=get_postdata();
		$wxapp_id=pdo_getcolumn('core_account_wxapp_live',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['live_id']),'wxapp_id');
		$data=array(
			'name'=>$post['name'],
			'cover_img'=>$post['cover_img'],
			'feeds_img'=>$post['feeds_img'],
			'start_time'=>strtotime($post['start_time']),
			'end_time'=>strtotime($post['end_time']),
			'anchor_name'=>$post['anchor_name'],
			'anchor_wechat'=>$post['anchor_wechat'],
			'shareimg'=>$post['shareimg'],
			'type'=>$post['type'],
			'sex'=>intval($post['sex']),
			'closelike'=>intval($post['closelike']),
			'closegoods'=>intval($post['closegoods']),
			'closecomment'=>intval($post['closecomment']),
			//'goods'=>ijson_encode($post['goods']);
		);
		if(!$data['name']){
			exi('请填写直播间名称！','error');
		}
		if(mb_strlen($data['name'])<3 || mb_strlen($data['name'])>17){
			exi('直播间名字，最短3个汉字，最长17个汉字！','error');
		}
		if(!$data['cover_img']){
			exi('请填写背景图！','error');
		}
		if(!$data['feeds_img']){
			exi('请填写视频封面图！','error');
		}
		if(!$data['shareimg']){
			exi('请填写分享图！','error');
		}
		if(!$data['anchor_name']){
			exi('请填写主播昵称！','error');
		}
		if(mb_strlen($data['anchor_name'])<2 || mb_strlen($data['anchor_name'])>15){
			exi('最短2个汉字，最长15个汉字！','error');
		}
		if(!$data['anchor_wechat']){
			exi('请填写正确的主播微信号！','error');
		}
		if(!$data['start_time'] || !$data['end_time']){
			exi('请填写开始时间和结束时间！','error');
		}
		if($data['start_time']-time()<10*60 || $data['start_time']-time()>6*30*24*3600){
			exi('开播时间需要在当前时间的10分钟后 并且 开始时间不能在 6 个月后','error');
		}
		if(($data['end_time']-$data['start_time']>24*3600) || $data['end_time']-$data['start_time']<30*60){
			exi('开播时间和结束时间间隔不得短于30分钟，不得超过24小时','error');
		}
		$params=array(
				'name'=>$post['name'],
				'coverImg'=>model('core/wxapp_api',$wxapp_id)->upload_media($post['cover_img'],'image'),
				'startTime'=>strtotime($post['start_time']),
				'endTime'=>strtotime($post['end_time']),
				'anchorName'=>$post['anchor_name'],
				'anchorWechat'=>$post['anchor_wechat'],
				'shareImg'=>model('core/wxapp_api',$wxapp_id)->upload_media($post['shareimg'],'image'),
				'feedsImg'=>model('core/wxapp_api',$wxapp_id)->upload_media($post['feeds_img'],'image'),
				'type'=>0,
				'closeLike'=>intval($post['closelike']),
				'closeGoods'=>intval($post['closegoods']),
				'closeComment'=>intval($post['closecomment']),
				'closeReplay'=>intval($post['closereplay']),
				'closeShare'=>intval($post['closeshare']),
				//'closeKf'=>intval($post['closekf']),
				//'goods'=>ijson_encode($post['goods']);
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('core_account_wxapp_live_rooms',$data,array('id'=>$id));
			if($post['roomid']){
				$params['id']=$post['roomid'];
				$res=model('core/wxapp_api',$wxapp_id)->edit_live_rooms($params);
				if($res['type']=='error'){
					exi('房间异常：'.$res['errmsg'],'error');
				}
			}
		}else{
			$res=model('core/wxapp_api',$wxapp_id)->add_live_rooms($params);
			if(!$res['roomId']){
				exi('创建房间异常：'.$res['errmsg'],'error');
			}
			$data['createtime']=TIMESTAMP;
			$data['uniacid']=$_SESSION['uniacid'];
			$data['live_id']=$post['live_id'];
			$data['roomid']=$res['roomId'];
			$data['mid']=$_SESSION['uid'];
			if($sale_live['ischeck']==1){
				$data['ischeck']=0;
			}else{
				$data['ischeck']=1;
			}
			pdo_insert('core_account_wxapp_live_rooms',$data);
		}
		exi('创建成功！','','jy_weishop/sale.live_myrooms/');
}